package com.example.uchain.service.impl;


import com.example.uchain.demo.Author;
import com.example.uchain.demo.PageBean;
import com.example.uchain.demo.Result;
import com.example.uchain.mapper.AuthorMapper;
import com.example.uchain.service.AuthorService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AuthorServicelmpl implements AuthorService {
    @Autowired
    private AuthorMapper authorMapper;


    @Override
    public List<Author> findAll() {
        return authorMapper.findAll();
    }

    @Override
    public int AddAuthor(Author author) {
        author.setUpdateTime(LocalDateTime.now());
        authorMapper.AddAuthor(author);
        long memberId = author.getMemberId();
        return (int) memberId;
    }

    @Override
    public Result deleteAuthorById(Long memberId) {
        if (authorMapper.findById(memberId) == null){
            log.info("该著权人不存在");
            return Result.error("该著权人不存在");
        }else {
            authorMapper.deleteAuthorById(memberId);
            return Result.success();
        }
        }



    @Override
    public void updateById(Author author) {
        author.setUpdateTime(LocalDateTime.now());
        authorMapper.updateById(author);
    }

    @Override
    public Author findAllById(Long memberId) {
      return authorMapper.findById(memberId);
    }

    @Override
    public PageBean pageForAut(Integer Page, Integer pageSize, String name, Long memberId, String phoneNumber, Integer code) {
        //设置分页参数
        PageHelper.startPage(Page, pageSize);

        //执行查询
        List<Author> authorList = authorMapper.findAllWithCode(name,memberId,phoneNumber,code);

        // 获取分页信息
        Page<Author> p = (Page<Author>)  authorList;

        //封装pageBean对象
        return new PageBean(p.getTotal(), p.getResult());
    }
}
